package com.lemon.smartsafety.ui.adapter.fire;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.result.fire.SuperviseCompanyAdd;
import com.lemon.smartsafety.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 项目名称：workspace
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/8/11 14:34
 * 修改人：Fargo
 * 修改时间：2016/8/11 14:34
 * 修改备注：   监督单位设置
 */
public class FireCompanySetAdapter extends BaseAdapter {

    /**
     * TAG
     */
    private final String TAG = "FireCompanySetAdapter";

    /**
     * 选择项
     */
    private int m_nSelectItem = -1;

    /**
     * 数据列表
     */
    private List<SuperviseCompanyAdd> mList;


    /**
     * 布局泵
     */
    private LayoutInflater m_layInflater = null;

    private Context mContext;

    private CallBackInterface mBack = null;

    public interface CallBackInterface {
        public void callBackFunction(int item);

    }

    public void setBack(CallBackInterface back) {
        this.mBack = back;
    }


    /**
     * 设置选择项
     *
     * @param n
     */
    public void setSelectItem(final int n) {
        m_nSelectItem = n;
    }

    /**
     * 设置数据列表
     */
    public void setList(ArrayList<SuperviseCompanyAdd> list) {
        this.mList = list;
    }

    /**
     * 构造函数
     *
     * @param context
     */
    public FireCompanySetAdapter(Context context, List<SuperviseCompanyAdd> list) {
        try {
            this.mContext = context;

            this.mList = list;

            // 布局泵
            m_layInflater = LayoutInflater.from(context);

        } catch (Exception e) {
        }
    }

    @Override
    public int getCount() {
        try {
            if (mList != null)
                return mList.size();
        } catch (Exception e) {
        }

        return 0;
    }

    @Override
    public Object getItem(int position) {
        try {
            if (mList != null)
                return mList.get(position);
        } catch (Exception e) {
        }

        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings({"deprecation"})
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        try {

            final ViewHolder holder;
            if (convertView == null) {
                convertView = m_layInflater.inflate(R.layout.item_fire_company_search, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            //内容
            holder.lineAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mBack != null) {
                        mBack.callBackFunction(position);
                    }
                }
            });

            try {
                holder.txtTitle.setText(mList.get(position).getCompanyName());
                holder.txtAddress.setText(mList.get(position).getCompanyAddress());
                holder.txtName.setText(mList.get(position).getPrincipalName());
                holder.txtPhone.setText(mList.get(position).getPrincipalPhone());
                if (!CommonUtil.strIsEmpty(mList.get(position).getSuperviseUserName())) {
                    holder.txtSuperviseUserName.setText(mList.get(position).getSuperviseUserName());
                    holder.lineSuperviseUserName.setVisibility(View.VISIBLE);
                } else {
                    holder.lineSuperviseUserName.setVisibility(View.GONE);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }


    static class ViewHolder {
        @Bind(R.id.txt_add)
        TextView txtAdd;
        @Bind(R.id.line_add)
        LinearLayout lineAdd;
        @Bind(R.id.txt_title)
        TextView txtTitle;
        @Bind(R.id.txt_address)
        TextView txtAddress;
        @Bind(R.id.txt_name)
        TextView txtName;
        @Bind(R.id.txt_phone)
        TextView txtPhone;
        @Bind(R.id.txt_SuperviseUserName)
        TextView txtSuperviseUserName;
        @Bind(R.id.line_SuperviseUserName)
        LinearLayout lineSuperviseUserName;
        @Bind(R.id.line_content)
        LinearLayout lineContent;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}